package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.commons.DateUtil;
import com.wnxy.pawnbackgroundsystem.commons.JwtUtils;
import com.wnxy.pawnbackgroundsystem.commons.ServiceImplUtil;
import com.wnxy.pawnbackgroundsystem.dao.BrandMapper;
import com.wnxy.pawnbackgroundsystem.dao.GoodsImgMapper;
import com.wnxy.pawnbackgroundsystem.dao.SortMapper;
import com.wnxy.pawnbackgroundsystem.dto.PageDto;
import com.wnxy.pawnbackgroundsystem.entity.Goods;
import com.wnxy.pawnbackgroundsystem.dao.GoodsMapper;
import com.wnxy.pawnbackgroundsystem.entity.User;
import com.wnxy.pawnbackgroundsystem.exception.DemoRuntimeException;
import com.wnxy.pawnbackgroundsystem.param.goodsParam.GoodsParam;
import com.wnxy.pawnbackgroundsystem.service.IGoodsImgService;
import com.wnxy.pawnbackgroundsystem.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.pawnbackgroundsystem.service.ISizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品详情表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private ISizeService sizeService;

    @Resource
    private IGoodsImgService goodsImgService;

    @Resource
    private SortMapper sortMapper;

    @Resource
    private BrandMapper brandMapper;

    @Override
    public IPage findAllGoodsByPage(GoodsParam goodsParam, PageDto pageDto) {
        log.info("查询所有商品业务层：查询所有商品信息接受到的数据goodsparam{},当前页{}，每页{}条,",goodsParam,pageDto.getPageIndex(),pageDto.getPageSize());
        Page page = new Page(pageDto.getPageIndex(),pageDto.getPageSize());
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsParam,goods);
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        if (null != goods) {
            if (null != goods.getGoodsName()) {
                qw.like("goods_name", goods.getGoodsName());
            }
            if (null != goods.getGoodsEnteringPerson()) {
                qw.like("goods_entering_person", goods.getGoodsEnteringPerson());
            }
            if (null != goods.getGoodsDefinePerson()) {
                qw.like("goods_define_person", goods.getGoodsDefinePerson());
            }
            if (null != goods.getGoodsAssessPerson()) {
                qw.like("goods_assess_person", goods.getGoodsAssessPerson());
            }
            if (null != goods.getGoodsStatus()) {
                qw.eq("goods_status", goods.getGoodsStatus());
            }
            if (null != goods.getSortId()) {
                qw.eq("sort_id", goods.getSortId());
            }
            if (null != goodsParam.getStartTime() && null != goodsParam.getEndTime()) {
                qw.lt("goods_entering_time",goodsParam.getEndTime() );
                qw.gt("goods_entering_time", goodsParam.getStartTime());
            }
            qw.orderByDesc(true, "goods_id");
        }
        IPage Ipage = goodsMapper.selectPage(page, qw);
        return Ipage;
    }

    @Override
    public Goods addGoods(GoodsParam goodsParam) {
        log.info("添加商品业务层：goodsparam:{}",goodsParam);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsParam,goods);
        User user = ServiceImplUtil.getJwtUtilUser(JwtUtils.AUTH_TOKEN_NAME);
        log.info("从jwt中拿去创建人的信息user:{}",user);
        goods.setGoodsCreatePerson(user.getUserName());
        goods.setGoodsCreateTime(System.currentTimeMillis());
        goods.setGoodsNum(goodsParam.getGoodsNum());
        int insert = goodsMapper.insert(goods);
        if (insert == 0) {
            throw new DemoRuntimeException("添加失败");
        }
        Goods goods1 = goodsMapper.selectById(goods.getGoodsId());
        if (null == goods1){
            throw new DemoRuntimeException("2002","添加成功，查找数据失败");
        }
        if (null != goodsParam.getMaterial()){
            sizeService.addSeze(goodsParam.getMaterial(),"材质",goods1.getGoodsId());
        }
        if (null != goodsParam.getColour()){
            sizeService.addSeze(goodsParam.getColour(),"颜色",goods1.getGoodsId());
        }
        if (null != goodsParam.getGoodsImgs()){
            goodsImgService.addGoodsImg(goods1.getGoodsId(),goodsParam.getGoodsName(),goodsParam.getGoodsImgs());
        }
        return goods1;
    }


    @Override
    public Integer updateGoods(GoodsParam goodsParam) {
       log.info("修改商品状态业务层：goodsparam:{}",goodsParam);
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("goods_id",goodsParam.getGoodsId());
        Goods goods = new Goods();
        goods.setGoodsStatus(goodsParam.getGoodsStatus());
        int i = goodsMapper.update(goods,qw);
        log.info("修改商品状态业务层返回参数：i:{}",i);
        return i;
    }

    @Override
    public Integer deleteGood(Integer ids) {
        log.info("删除商品状态业务层：goodsparam:{}",ids);
        int idss = goodsMapper.deleteById(ids);

        return idss;
    }


}
